package org.example.t0526;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class T4 {

    public static void main(String[] args) {
        char [] a1 = "1234567".toCharArray();
        char [] a2 = "ABCDEFG".toCharArray();

        Lock lock = new ReentrantLock();//声明重入锁
        Condition condition1 = lock.newCondition();//声明一个锁条件
        Condition condition2 = lock.newCondition();//声明一个锁条件

        new Thread(() ->{
            try {
            lock.lock();
            for(char a : a1){
                System.out.print(a);
                condition2.signal();//通知conditio2执行
                condition1.await();//让本线程停止，进入等待队列
              }
                condition2.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();//释放锁
            }
        },"t1").start();

        new Thread(() ->{
            try {
            lock.lock();
            for(char a : a2){
                System.out.print(a);
                condition1.signal();
                condition2.await();
             }
                condition1.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }

        },"t2").start();
    }
}
